package com.wmjsoft.plugins.security.base

import org.slf4j.LoggerFactory
import org.springframework.beans.BeanUtils
import java.lang.reflect.Field
import java.lang.reflect.Modifier
import java.math.BigDecimal
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.LocalDateTime
import java.util.*

/**
 * 新增或修改的判断
 */
fun < T : Any> saveOrUpdate(t: T, myBaseService: MyBaseService<T>): T {
    val log = LoggerFactory.getLogger(t::class.java)
    var id = ClassUtils.getProperty(t, "id")
    try {
        if (null == id) {

            myBaseService.save(t)
        } else {
            id = id as Long
            if(id  == 0){
                myBaseService.save(t)
            }else{
                myBaseService.updateById(t)
            }
        }
        return t
    } catch (e: java.lang.Exception) {
        e.printStackTrace()
        log.error("---------------------------保存或修改出现异常---------------clazzName${t::class.java.name},vto:${t.toString()}")
        throw RuntimeException("新增或修改发生异常，请联系后台工作人员")
    }
}




/**
 * 用于copy对象
 * @program: bridge
 * @description:
 * @author: Mr.Lee
 * @create: 2020-10-09 09:37
 */
fun <S, T> myConvert(source: S, target: T): T {
    BeanUtils.copyProperties(source!!, target!!)
    return target
}


/**
 * 断言
 */
fun isFalse(flag: Boolean, message: String) {
    if (!flag) {
        throw RuntimeException(message)
    }
}

fun <T> isNull(t: T, message: String) {
    if (null == t) {
        throw RuntimeException(message)
    }
}

fun <T> isBlank(t: T): Boolean {
    if (null == t || "" == t) {
        return false
    }
    return true
}

fun <E> isEmpty(collection: Collection<E>, message: String) {
    if (collection.isEmpty()) {
        throw RuntimeException(message)
    }
}



/**
 * 转换dto
 */
fun <T, TREE> tConversionDto(ts: List<T>, dto: Class<TREE>): ArrayList<TREE> {
    var trees = ArrayList<TREE>()
    ts.forEach {
        var t = dto.getConstructor().newInstance()
        trees.add(myConvert(it, t))
    }
    return trees
}


/**
 * 测试专用
 */
fun <T> tNew(tClazz: Class<T>): T {
    val t = tClazz.getConstructor().newInstance()
    // 得到类对象
    val fs: Array<Field> = tClazz.declaredFields
    for (i in fs.indices) {
        val f: Field = fs[i]

        // 设置些属性是可以访问的
        val isStatic: Boolean = Modifier.isStatic(f.modifiers)
        if (isStatic) {
            continue
        }
        // 设置些属性是可以访问的
        f.isAccessible = true
        try {
            // 得到此属性的值
            val v: Any? = null
            // 得到此属性的类型
            val type: String = f.type.toString()
            if (type.endsWith("String") && v == null) {
                // 给属性设值
                f.set(t, "test")
            } else if ((type.endsWith("int")
                            || type.endsWith("Integer")
                            || type.endsWith("double"))
                    && v == null) {
                f.set(t, 0)
            } else if ((type.endsWith("long") || type.endsWith("Long")) && v == null) {
                f.set(t, 0L)
            } else if (type.endsWith("Date") && v == null) {
                f.set(t, LocalDate.now())
            } else if (type.endsWith("Timestamp") && v == null) {
                f.set(t, LocalDateTime.now())
            } else if (type.endsWith("BigDecimal") && v == null) {
                f.set(t, BigDecimal(0))
            } else if (type.endsWith("LocalDateTime")) {
                f.set(t, LocalDateTime.now())
            } else if (type.endsWith("Short")) {
                f.set(t, 0.toShort())
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
    return t
}




/**
 * 时间格式化
 * YYYY年MM月dd日
 */
fun dateTimeFormat(date: Date?): String {
    val f = SimpleDateFormat("yyyy年MM月dd日")
    return f.format(date)
}

/**
 * 时间格式化
 * YYYY年MM
 */
fun dateFormat(date: LocalDate): String {
    val f = SimpleDateFormat("yyyy年MM月")
    return f.format(date)
}


